package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.Sensibilizacion;

public class SensibilizacionDAOImpl extends JdbcDaoSupport implements ISensibilizacionDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;

    private FindByActiveStateQuery findByActiveStateQuery;
    
    private JdbcTemplate jdbcTemplate;

	private static final String SELECT = "SELECT * FROM GES_SENSIBILIZACION_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";
	
	//TOUCH

    public void delete(Sensibilizacion sensibilizacion) {
		deleteOperation.execute( sensibilizacion  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public Sensibilizacion findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(Sensibilizacion sensibilizacion) {
		insertOperation.execute( sensibilizacion );
	}

	public void update(Sensibilizacion sensibilizacion) {
		updateOperation.execute( sensibilizacion );
	}

	public Sensibilizacion findByCriteria(Sensibilizacion  sensibilizacion) {
		return findByCriteriaQuery.runQuery(sensibilizacion);
	}

	public List load(Sensibilizacion sensibilizacion) {
		return findAllQuery.runQuery(sensibilizacion);
	}

	public Sensibilizacion findByActiveState(Sensibilizacion  sensibilizacion) {
		return findByActiveStateQuery.runQuery(sensibilizacion);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	deleteOperation = new DeleteOperation(  getDataSource() );
    	deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    	jdbcTemplate = new JdbcTemplate( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO GES_SENSIBILIZACION_T ("
				  + " ID, "	
				  
				  + " ID_SENSIBILIZACION_TIPO, "
				  + " ID_AREA, "
				  + " CODIGO, "
				  
				  + " FECHA, "
				  
				  + " DIRECCION_RESIDENCIA, "
				  + " CODIGO_BARRIO, "
				  + " BARRIO, "
				  + " CODIGO_COMUNA, "
				  + " COMUNA, "
				  + " LUGAR, "
				  + " ESTABLECIMIENTO, "
				  + " TELEFONO, "
				  + " ESTADO, "
				  
				  + " USUARIO_CREA, "
				  + " FECHA_CREA)" 
				  
				  + " VALUES ("
				  
			  	  + " GES_SENSIBILIZACION_T_SEC.nextVal, "
			  	  
			  	  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  
				  + PARAMETER
				  
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  

				  + PARAMETER
				  + "SYSDATE "
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			
			declareParameter( new SqlParameter(Types.DATE));
			
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.CHAR));
			//declareParameter( new SqlParameter(Types.DATE));
			compile();
		}
		public void execute(Sensibilizacion sensibilizacion) {
			InsertOperation.this.update( new Object[]{
				
				sensibilizacion.getIdSensibilizacionTipo(),
				sensibilizacion.getIdArea(),
				sensibilizacion.getCodigo(),
				
				sensibilizacion.getFecha(),
				
				sensibilizacion.getDireccionResidencia(),
				sensibilizacion.getCodigoBarrio(),
				sensibilizacion.getBarrio(),
				sensibilizacion.getCodigoComuna(),
				sensibilizacion.getComuna(),
				sensibilizacion.getLugar(),
				sensibilizacion.getEstablecimiento(),
				sensibilizacion.getTelefono(),
				sensibilizacion.getEstado(),
				
				sensibilizacion.getUsuarioCrea()
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE GES_SENSIBILIZACION_T SET "
					  + " ID_SENSIBILIZACION_TIPO = ? ,"
					  + " ID_AREA = ? ,"
					  + " CODIGO = ? ,"
					  
					  + " FECHA = ? ,"
					  
					  + " DIRECCION_RESIDENCIA = ? ,"
					  + " CODIGO_BARRIO = ? ,"
					  + " BARRIO = ? ,"
					  + " CODIGO_COMUNA = ? ,"
					  + " COMUNA = ? ,"
					  + " LUGAR = ? ,"
					  + " ESTABLECIMIENTO = ? ,"
					  + " TELEFONO = ? ,"
					  + " ESTADO = ? ,"
					  
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE "
				  	  + WHERE 
				  	  + "ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			
			declareParameter( new SqlParameter(Types.DATE));
			
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.CHAR));
			
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(Sensibilizacion sensibilizacion) {
			UpdateOperation.this.update( new Object[]{
				
				sensibilizacion.getIdSensibilizacionTipo(),
				sensibilizacion.getIdArea(),
				sensibilizacion.getCodigo(),
				
				sensibilizacion.getFecha(),
				
				sensibilizacion.getDireccionResidencia(),
				sensibilizacion.getCodigoBarrio(),
				sensibilizacion.getBarrio(),
				sensibilizacion.getCodigoComuna(),
				sensibilizacion.getComuna(),
				sensibilizacion.getLugar(),
				sensibilizacion.getEstablecimiento(),
				sensibilizacion.getTelefono(),
				sensibilizacion.getEstado(),
				
				sensibilizacion.getUsuarioModifica(),
				
				sensibilizacion.getId(),
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM GES_SENSIBILIZACION_T "
										+ WHERE
			  	  	  					+ " CODIGO = ? ";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
		}
		public void execute(Sensibilizacion sensibilizacion) {
			this.update( new Object[]{
				sensibilizacion.getCodigo(),
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM GES_SENSIBILIZACION_T "
										+ WHERE
										+ " 1 = 1 ";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			Sensibilizacion sensibilizacion = new Sensibilizacion();
				sensibilizacion.setId(new Long(rs.getLong("ID")));
				sensibilizacion.setIdSensibilizacionTipo(new Long(rs.getLong("ID_SENSIBILIZACION_TIPO")));
				sensibilizacion.setIdArea(new Long(rs.getLong("ID_AREA")));
				sensibilizacion.setCodigo(rs.getString("CODIGO"));
				
				sensibilizacion.setFecha(rs.getDate("FECHA"));		
				sensibilizacion.setDireccionResidencia(rs.getString("DIRECCION_RESIDENCIA"));
				sensibilizacion.setCodigoBarrio(rs.getString("CODIGO_BARRIO"));
				sensibilizacion.setBarrio(rs.getString("BARRIO"));
				sensibilizacion.setCodigoComuna(rs.getString("CODIGO_COMUNA"));
				sensibilizacion.setComuna(rs.getString("COMUNA"));
				sensibilizacion.setLugar(rs.getString("LUGAR"));
				sensibilizacion.setEstablecimiento(rs.getString("ESTABLECIMIENTO"));
				sensibilizacion.setTelefono(rs.getString("TELEFONO"));
				sensibilizacion.setEstado(rs.getString("ESTADO"));
				
				sensibilizacion.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				sensibilizacion.setFechaCrea(rs.getDate("FECHA_CREA"));		
				sensibilizacion.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				sensibilizacion.setFechaModifica(rs.getDate("FECHA_MODIFICA"));		
				
			return sensibilizacion;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = "SELECT"
											+ WHERE + " ID = ?";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
    	}
    	public Sensibilizacion runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (Sensibilizacion) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT + "*"
										+ WHERE + " 1=1 ";
			//For required criteria elements uncomment required values
			  	  	  					//+ " AND ID = ? "
			  	  	  					//+ " AND ID_SENSIBILIZACION_TIPO = ? "
			  	  	  					//+ " AND ID_AREA = ? "
			  	  	  					//+ " AND CODIGO = ? "
			  	  	  					//+ " AND FECHA = ? "
			  	  	  					//+ " AND DIRECCION_RESIDENCIA = ? "
			  	  	  					//+ " AND CODIGO_BARRIO = ? "
			  	  	  					//+ " AND BARRIO = ? "
			  	  	  					//+ " AND CODIGO_COMUNA = ? "
			  	  	  					//+ " AND COMUNA = ? "
			  	  	  					//+ " AND LUGAR = ? "
			  	  	  					//+ " AND ESTABLECIMIENTO = ? "
			  	  	  					//+ " AND TELEFONO = ? "
			  	  	  					//+ " AND ESTADO = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
			  	  	  					//+ " AND USUARIO_MODIFICA = ? "
			  	  	  					//+ " AND FECHA_MODIFICA = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			compile();
    	}
    	public Sensibilizacion runQuery(Sensibilizacion sensibilizacion) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//sensibilizacion.getId(),
				//sensibilizacion.getIdSensibilizacionTipo(),
				//sensibilizacion.getIdArea(),
				//sensibilizacion.getCodigo(),
				//sensibilizacion.getFecha(),
				//sensibilizacion.getDireccionResidencia(),
				//sensibilizacion.getCodigoBarrio(),
				//sensibilizacion.getBarrio(),
				//sensibilizacion.getCodigoComuna(),
				//sensibilizacion.getComuna(),
				//sensibilizacion.getLugar(),
				//sensibilizacion.getEstablecimiento(),
				//sensibilizacion.getTelefono(),
				//sensibilizacion.getEstado(),
				//sensibilizacion.getUsuarioCrea(),
				//sensibilizacion.getFechaCrea(),
				//sensibilizacion.getUsuarioModifica(),
				//sensibilizacion.getFechaModifica(),
				});
			if ( result.size() > 0 ) {
        		return (Sensibilizacion) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE 
										+ "1=1 "
										+ "AND ID_SENSIBILIZACION_TIPO = ? "
										+ "AND ID_AREA = ? "
										+ "AND CODIGO LIKE ? ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.INTEGER) );
    		declareParameter( new SqlParameter(Types.INTEGER) );
    		declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public List runQuery(Sensibilizacion sensibilizacion) {
			List result =  execute(new Object[]{
					sensibilizacion.getIdSensibilizacionTipo(),
					sensibilizacion.getIdArea(),
					new String(sensibilizacion.getCodigo()!=null&&!sensibilizacion.getCodigo().equals("")? sensibilizacion.getCodigo():"%")
					});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = "SELECT"
										+ WHERE  + " 1=1";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public Sensibilizacion runQuery(Sensibilizacion sensibilizacion) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (Sensibilizacion) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }
    
    public String getLastSequenceValue() {
        return (String) this.jdbcTemplate.queryForObject("SELECT last_number FROM user_sequences  WHERE  SEQUENCE_NAME = 'GES_SENSIBILIZACION_T_SEC'", String.class);
    }

}
